package com.zxkj.commonpatrol.adapt;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentManager;

import com.SuperKotlin.pictureviewer.ImagePagerActivity;
import com.chad.library.adapter.base.BaseDelegateMultiAdapter;

import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cunoraz.gifview.library.GifView;
import com.czy.basecommon.utils.tools.ActivityTool;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.luck.picture.lib.PictureSelector;
import com.xuexiang.xui.widget.dialog.bottomsheet.BottomSheet;
import com.xuexiang.xui.widget.imageview.RadiusImageView;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.entity.MyMediaEntry;
import com.zxkj.commonpatrol.model.entity.itemPicEntity;
import com.zxkj.commonpatrol.model.entity.itemVideoEntity;
import com.zxkj.commonpatrol.model.entity.itemVioceEntity;
import com.zxkj.commonpatrol.ui.AddExceptionActivity;
import com.zxkj.commonpatrol.ui.NewExceptionActivity;
import com.zxkj.commonpatrol.utils.MediaManager;
import com.zxkj.commonpatrol.view.VioceDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class MyMediaAdapt extends BaseDelegateMultiAdapter<MyMediaEntry, BaseViewHolder> {
    private AddExceptionActivity activity;
    private  int gruopIndex=0;
    private ArrayList<String> collect;
    FragmentManager FragManager ;
    public MyMediaAdapt(FragmentManager getSupportFragmentManager, int gruopIndex, AddExceptionActivity activity) {

        super(null);

        this.activity=activity;
        this.gruopIndex=gruopIndex;
        this.FragManager=getSupportFragmentManager;

        setMultiTypeDelegate(new BaseMultiTypeDelegate<MyMediaEntry>() {
            @Override
            public int getItemType(@NonNull List<? extends MyMediaEntry> data, int position) {
                // 根据数据，自己判断应该返回的类型
                switch (data.get(position).getType()) {

                    case 1:
                        return 1;
                    case 2:
                        return 2;
                    case 3:
                        return 3;

                    default:
                        return  0;

                }




            }
        });
        // 第二部，绑定 item 类型
        getMultiTypeDelegate()
                .addItemType(1, R.layout.item_pic)//金色
                .addItemType(2, R.layout.item_voice)//红色
                .addItemType(3, R.layout.item_ve);//黑色


    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void convert(@NonNull BaseViewHolder helper, MyMediaEntry item) {


        //Step.3
        switch (helper.getItemViewType()) {
            case MyMediaEntry.IMG:
                RadiusImageView iv = (RadiusImageView) helper.itemView.findViewById(R.id.pic1);
                iv.setImageURI(Uri.fromFile(new File(item.getItemPicEntity().getPicPath())));
                iv.setOnClickListener(v->{
                    new BottomSheet.BottomListSheetBuilder(getContext())

                            .addItem("查看大圖")
                            .addItem("刪除")
                            .setIsCenter(true)
                            .setOnSheetItemClickListener(new BottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                                @RequiresApi(api = Build.VERSION_CODES.N)
                                @Override
                                public void onClick(BottomSheet dialog, View itemView, int position, String tag) {
                                    dialog.dismiss();
                                    List<itemPicEntity> images = getData().stream().filter(p -> p.getItemPicEntity() != null).map(p -> p.getItemPicEntity()).collect(Collectors.toList());
                                    List<String> collects = images.stream().map(p -> p.getPicPath()).collect(Collectors.toList());
                                    collect=new ArrayList<>();
                                    collect.addAll(collects);

                                    if (position == 0) {
                                        com.SuperKotlin.pictureviewer.PictureConfig config = new com.SuperKotlin.pictureviewer.PictureConfig.Builder()
                                                .setListData(collect)    //图片数据List<String> list
                                                .setPosition(helper.getLayoutPosition())    //图片下标（从第position张图片开始浏览）
                                                .setIsShowNumber(true)//是否显示数字下标
                                                .needDownload(false)    //是否支持图片下载
                                                .setPlacrHolder(R.drawable.nodatas)    //占位符图片（图片加载完成前显示的资源图片，来源drawable或者mipmap）
                                                .build();
                                        ImagePagerActivity.startActivity(getContext(), config);
                                    } else {
                                        LiveEventBus.get("mPhotoSizes").post(1);
                                        remove(helper.getLayoutPosition());
                                    }
                                }
                            })
                            .build()
                            .show();
                });


                // do something
                break;
            case MyMediaEntry.VIOCE:
                GifView vioce = (GifView) helper.itemView.findViewById(R.id.voice1);
                vioce.pause();


                helper.itemView.setOnClickListener(view -> {

                    new BottomSheet.BottomListSheetBuilder(getContext())
                            .addItem("播放音頻")
                            .addItem("刪除")
                            .setIsCenter(true)
                            .setOnSheetItemClickListener(new BottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                                @Override
                                public void onClick(BottomSheet dialog, View itemView, int position, String tag) {
                                    dialog.dismiss();
                                    if (position == 0) {
                                        VioceDialog vioceDialog = VioceDialog.getInstance();
                                        vioceDialog.show(FragManager,"");

                                        MediaManager.playSound(item.getVioceEntity().getViocePath(), new MediaPlayer.OnCompletionListener() {
                                            @Override
                                            public void onCompletion(MediaPlayer mediaPlayer) {
                                                vioceDialog.dismiss();


                                            }
                                        });
                                    } else {

                                        setInitAudioRecord();

                                        remove(helper.getLayoutPosition());


                                    }
                                }
                            })
                            .build()
                            .show();
                });
                // do something
                break;
            case MyMediaEntry.VIDEO:
                RadiusImageView iv1 = (RadiusImageView) helper.itemView.findViewById(R.id.ve);
                iv1.setImageURI(Uri.fromFile(new File(item.getItemVideoEntity().getFristPath())));

                iv1.setOnClickListener(v -> {

                    new BottomSheet.BottomListSheetBuilder(getContext())
                            .addItem("播放視頻")
                            .addItem("刪除")
                            .setIsCenter(true)
                            .setOnSheetItemClickListener(new BottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                                @Override
                                public void onClick(BottomSheet dialog, View itemView, int position, String tag) {
                                    dialog.dismiss();
                                    if (position == 0) {
                                        //播放
                                        PictureSelector.create(ActivityTool.getInstance().getLastActivity()).externalPictureVideo(item.getItemVideoEntity().getVideoPath());
                                    } else {

                                        remove(helper.getLayoutPosition());


                                    }
                                }
                            })
                            .build()
                            .show();
                });


                // do something
                break;
        }
    }
   public void  MyNotifyDataSetChanged(){
       setInitAudioRecord();
       notifyDataSetChanged();
   }


   /**
    *desc:将audiobutton 初始化
    *author:mid
    *addtime:2020-01-16 16:15
    */
    private void setInitAudioRecord() {
        switch (gruopIndex) {
            case 1:
                //第一组
                activity.getGroup1MyAudioRecorderButton().setCountss(0);

                break;
            case 2:
                activity.getGroup2MyAudioRecorderButton().setCountss(0);
                break;
            case 3:
                activity.getGroup3MyAudioRecorderButton().setCountss(0);
                break;
        }
    }

    public List<itemPicEntity> getPic(){
        List<itemPicEntity> picEntities=new ArrayList<>();
        List<MyMediaEntry> data = getData();
        for (MyMediaEntry entry : data) {
            if (entry.getItemPicEntity()!=null&&!TextUtils.isEmpty(entry.getItemPicEntity().getPicPath())){
                picEntities.add(entry.getItemPicEntity());
            }

        }
        return picEntities;
    }
    public List<itemVideoEntity> getVideo(){
        List<itemVideoEntity> picEntities=new ArrayList<>();
        List<MyMediaEntry> data = getData();
        for (MyMediaEntry entry : data) {
            if (entry.getItemVideoEntity()!=null&&!TextUtils.isEmpty(entry.getItemVideoEntity().getVideoPath())){
                picEntities.add(entry.getItemVideoEntity());
            }

        }
        return picEntities;
    }
    public List<itemVioceEntity> getVioce(){
        List<itemVioceEntity> picEntities=new ArrayList<>();
        List<MyMediaEntry> data = getData();
        for (MyMediaEntry entry : data) {
            if (entry.getVioceEntity()!=null&&!TextUtils.isEmpty(entry.getVioceEntity().getViocePath())){
                picEntities.add(entry.getVioceEntity());
            }

        }
        return picEntities;
    }

}
